import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/* Router Modules */
import tableRouter from './modules/table'
import nestedRouter from './modules/nested'

export const constantRoutes = [{
  path: '/redirect',
  component: Layout,
  hidden: true,
  children: [{
    path: '/redirect/:path(.*)',
    component: () => import('@/views/redirect/index')
  }]
},
{
  path: '/login',
  component: () => import('@/views/login/index'),
  hidden: true
},
{
  path: '/auth-redirect',
  component: () => import('@/views/login/auth-redirect'),
  hidden: true
},
{
  path: '/404',
  component: () => import('@/views/error-page/404'),
  hidden: true
},
{
  path: '/401',
  component: () => import('@/views/error-page/401'),
  hidden: true
},

{
  path: '/',
  component: Layout,
  redirect: '/dashboard',
  children: [{
    path: 'dashboard',
    component: () => import('@/views/dashboard/index'),
    name: 'Dashboard',
    meta: {
      title: '首页',
      icon: 'dashboard',
      affix: true
    }
  }]
},
{
  path: '/',
  component: Layout,
  redirect: '/anfang',
  children: [{
    path: 'anfang',
    component: () => import('@/views/anfang/dong/dong'),
    name: 'anfang',
    meta: {
      title: '安防管理',
      icon: '安防管理',
      affix: true
    }
  }]
},
// 路由嵌套
nestedRouter,
{
  path: '/profile',
  component: Layout,
  redirect: '/profile/index',
  hidden: true,
  children: [{
    path: 'index',
    component: () => import('@/views/profile/index'),
    name: 'Profile',
    meta: {
      title: 'Profile',
      icon: 'user',
      noCache: true
    }
  }]
}

]

/**
 * asyncRoutes
 * 需要根据用户角色动态加载的路由
 */
export const asyncRoutes = [{
  path: '/permission',
  component: Layout,
  redirect: '/permission/page',
  alwaysShow: true, // 将始终显示根菜单
  name: 'Permission',
  meta: {
    title: '权限测试页',
    icon: 'lock',
    roles: ['admin', 'editor'] // 可以在根导航中设置角色
  },
  children: [{
    path: 'page',
    component: () => import('@/views/permission/page'),
    name: 'PagePermission',
    meta: {
      title: '页面权限',
      roles: ['admin'] // 或者你只能在潜艇导航中设置角色
    }
  },
  {
    path: 'directive',
    component: () => import('@/views/permission/directive'),
    name: 'DirectivePermission',
    meta: {
      title: '指令权限'
      // 如果不设置角色，表示:该页面不需要权限
    }
  },
  {
    path: 'role',
    component: () => import('@/views/permission/role'),
    name: 'RolePermission',
    meta: {
      title: '角色权限',
      roles: ['admin']
    }
  }
  ]
},

{
  path: '/icon',
  component: Layout,
  children: [{
    path: 'index',
    component: () => import('@/views/icons/index'),
    name: 'Icons',
    meta: {
      title: '图标',
      icon: 'icon',
      noCache: true
    }
  }]
},

/** 当你的路由图太长时，你可以把它分成小模块 **/
tableRouter,

// {
//   path: '/error',
//   component: Layout,
//   redirect: 'noRedirect',
//   name: 'ErrorPages',
//   meta: {
//     title: '错误页面',
//     icon: '404'
//   },
//   children: [{
//       path: '401',
//       component: () => import('@/views/error-page/401'),
//       name: 'Page401',
//       meta: {
//         title: '401',
//         noCache: true
//       }
//     },
//     {
//       path: '404',
//       component: () => import('@/views/error-page/404'),
//       name: 'Page404',
//       meta: {
//         title: '404',
//         noCache: true
//       }
//     }
//   ]
// },

{
  path: '/excel',
  component: Layout,
  redirect: '/excel/export-excel',
  name: 'Excel',
  meta: {
    title: 'Excel',
    icon: 'excel'
  },
  children: [{
    path: 'export-excel',
    component: () => import('@/views/excel/export-excel'),
    name: 'ExportExcel',
    meta: {
      title: '导出 Excel'
    }
  },
  {
    path: 'export-selected-excel',
    component: () => import('@/views/excel/select-excel'),
    name: 'SelectExcel',
    meta: {
      title: '导出 已选择项'
    }
  },
  {
    path: 'export-merge-header',
    component: () => import('@/views/excel/merge-header'),
    name: 'MergeHeader',
    meta: {
      title: '导出 多级表头'
    }
  },
  {
    path: 'upload-excel',
    component: () => import('@/views/excel/upload-excel'),
    name: 'UploadExcel',
    meta: {
      title: '上传 Excel'
    }
  }
  ]
},

// 04页面必须放在最后 !!!
{
  path: '*',
  redirect: '/404',
  hidden: true
}
]

const createRouter = () => new Router({
  mode: 'history', // 需要服务支持
  scrollBehavior: () => ({
    y: 0
  }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
